var config = require('../config.js')
function Get (url, data, cb) {
  this.isLogin(url);
	wx.showNavigationBarLoading();  
	wx.request({
    method:'GET',
		url: config.BASE_URL + url,
		data: data,
    header: {
      "U-TOKEN": wx.getStorageSync('user') ? wx.getStorageSync('user').token : url,
    },
		success:(res) => {
      if (res.code==401) {
        wx.setStorageSync('user',null);
      }
			typeof cb == "function" && cb(res.data,"");
			wx.hideNavigationBarLoading();
		},
		fail:(err) => {
			typeof cb == "function" && cb(null,err.errMsg);
			wx.hideNavigationBarLoading();
		}
	});
};
function Post (url, data, cb) {
  this.isLogin(url);
	wx.request({
    method:'POST',
		url: config.BASE_URL + url,
		data: data,
    header: {
      "U-TOKEN": wx.getStorageSync('user') ? wx.getStorageSync('user').token : url,
    },
		success:(res) => {
      if (res.code==401) {
        wx.setStorageSync('user',null);
      }
			typeof cb == "function" && cb(res.data,"");
		},
		fail:(err) => {      
			typeof cb == "function" && cb(null,err.errMsg);
		}
	});
};
function PutForm (url, data, cb) {
  this.isLogin(url);
	wx.request({
    method:'PUT',
		url: config.BASE_URL + url,    
		data: data,
    header: {
      "U-TOKEN": wx.getStorageSync('user') ? wx.getStorageSync('user').token : url,
      'content-type': 'application/x-www-form-urlencoded'
    },
		success:(res) => {
      if (res.code==401) {
        wx.setStorageSync('user',null);
      }
			typeof cb == "function" && cb(res.data,"");
		},
		fail:(err) => {
			typeof cb == "function" && cb(null,err.errMsg);
		}
	});
};
function Put (url, data, cb) {
  this.isLogin(url);
	wx.request({
    method:'PUT',
		url: config.BASE_URL + url,
		data: data,
    header: {
      "U-TOKEN": wx.getStorageSync('user') ? wx.getStorageSync('user').token : url,
      'content-type': 'application/json'
    },
		success:(res) => {
      if (res.code==401) {
        wx.setStorageSync('user',null);
      }
			typeof cb == "function" && cb(res.data,"");
		},
		fail:(err) => {
			typeof cb == "function" && cb(null,err.errMsg);
		}
	});
};
function Upload (url, file, data, cb) {
  this.isLogin(url);
	wx.uploadFile({
		url:  config.BASE_URL + url,
		filePath: file,
		method: 'POST',
    name: 'file',
    header: {  
      "U-TOKEN": wx.getStorageSync('user') ? wx.getStorageSync('user').token : url,
      'Content-Type': 'multipart/form-data'                    
    },
		formData:data,
		success:(res) => {
      if (res.code==401) {
        wx.setStorageSync('user',null);
      }
			if( typeof(res.data)=="string") {
				typeof cb == "function" && cb( JSON.parse(res.data),"");
			}else {
				typeof cb == "function" && cb(res.data,"");	
			}
		},
		fail:(err) => {
			typeof cb == "function" && cb(null,err.errMsg);
		}
	});
};
function putOpenid(code, cb) {
  wx.request({
    method:'GET',
		url: config.BASE_URL + config.clubApi.putOpenid,
		data: {code:code, appid:config.APPID},
    header: {
      "U-TOKEN": wx.getStorageSync('user') ? wx.getStorageSync('user').token : config.clubApi.putOpenid,
    },
		success:(res) => {
      if (res.code==401) {
        wx.setStorageSync('user',null);
      }
			typeof cb == "function" && cb(res.data,"");
			wx.hideNavigationBarLoading();
		},
		fail:(err) => {
			typeof cb == "function" && cb(null,err.errMsg);
			wx.hideNavigationBarLoading();
		}
	});  
};
function isLogin(url){
  let permitURLs = ["/userapp/getProductTypes","/userapp/getShopList",
  "/userapp/getShopInfo","/userapp/getShopAllList","/userapp/getProductList",
  "/userapp/getProductInfo","/userapp/getTeacherList","/userapp/getTeacherInfo",
  "/userapp/getCourseList","/userapp/getCourseInfo","/userapp/getPhone","/userapp/wxUser"];
  if (permitURLs.indexOf(url)>=0) {
    return true;
  }else {
    if (!wx.getStorageSync('user')||!wx.getStorageSync('user').token) {
      return wx.switchTab({ url: '/pages/user/index' });
    }
  }
}
module.exports ={
  httpGet:Get,
  httpPost:Post,
  httpPut:Put,
  httpPutForm:PutForm,
	httpUpload:Upload,
  putOpenid:putOpenid,
  isLogin:isLogin
};